<?php
/**
 * Created by PhpStorm.
 * User: FDG296
 * Date: 2017/8/2
 * Time: 16:42
 */
namespace common\models;

use Yii;
use yii\db\ActiveRecord;

class GoodsSource extends ActiveRecord{

    public static function tableName(){
        return '{{%goods_source}}';
    }

    public function getSourceList($goods_id){
        $result = array();
        $bath_url = Yii::$app->params['imageBathUrl'] . '/';
        $where = ['s.goods_id'=>$goods_id];
        $list = $this::find()->alias('s')->leftJoin('j_goods_sale as g','s.goods_id = g.goods_id')
            ->where($where)->orderBy('s.sort asc')->select('s.id,s.source_type,s.source_url,s.source_link,g.goods_name_cn')->asArray()->all();
        if(!empty($list)){
            foreach($list as &$value){
                if($value['source_type'] == 1){
                    $value['source_url'] = $bath_url . 'uploads/2017-09-26/150639543251916.png';
                }else{
                    $value['source_url'] = $bath_url . $value['source_url'];
                }
                $value['source_link'] = $bath_url . $value['source_link'];
            }
            $result = $list;
        }
        return $result;
    }

    public function getSourceBrGoodsId($goods_id_list){
        $result = array();
        $where = ['goods_id'=>$goods_id_list,'use_flg'=>1,'source_type'=>0];
        $list = $this::find()->where($where)->orderBy('sort asc')->groupBy('goods_id')->asArray()->all();
        if(!empty($list)){
            foreach($list as $value){
                $result[$value['main_goods_id']][] = $value;
            }
        }
        return $result;
    }
}